package com.qujia.customer.controller;
import com.qujia.common.constant.HttpStatus;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.core.page.PageDomain;
import com.qujia.common.core.page.TableSupport;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.StringUtils;
import com.qujia.common.utils.poi.ExcelUtil;
import com.qujia.customer.domain.CustomerManage;
import com.qujia.customer.service.ICustomerManageService;
import com.qujia.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qujia.common.annotation.Log;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.enums.BusinessType;

import com.qujia.common.core.page.TableDataInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 客户管理Controller
 *
 * @author qujia
 * @date 2020-12-21
 */
@RestController
@RequestMapping("/customer/manage")
public class CustomerManageController extends BaseController
{
    @Autowired
    private ICustomerManageService customerManageService;
    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询客户管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerManage customerManage)
    {
        List<String> list = deptService.levelDeptList(customerManage.getLevelId());
        if(list!=null && list.size()>0){
            customerManage.setStationIdList(list);
            customerManage.setStationId(null);
        }
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
           if(pageNum>0){
               pageNum =(pageNum-1)*pageSize;
           }
            customerManage.setPageNumLimit(pageNum);
            customerManage.setPageSizeLimit(pageSize);
        }
        List<CustomerManage> customerManageList = customerManageService.selectCustomerManage(customerManage);
        List<CustomerManage> pagelist = customerManageService.selectCustomerManagePage(customerManage);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(customerManageList);
        rspData.setTotal(pagelist.size());
        return rspData;
    }

    /**
     * 导出客户管理列表
     */
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CustomerManage customerManage)
    {
        List<String> list = deptService.levelDeptList(customerManage.getLevelId());
        if(list!=null && list.size()>0){
            customerManage.setStationIdList(list);
            customerManage.setStationId(null);
        }
        List<CustomerManage> customerManageList = customerManageService.selectCustomerManageList(customerManage);
        ExcelUtil<CustomerManage> util = new ExcelUtil<CustomerManage>(CustomerManage.class);
        return util.exportExcel(customerManageList, "manage");
    }

    /**
     * 获取客户管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerManageService.selectCustomerManageById(id));
    }

    /**
     * 新增客户管理
     */
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerManage customerManage)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerManage.setStationId(currentUser.getUser().getDeptId());
        customerManage.setStationName(currentUser.getUser().getDept().getDeptName());
        customerManage.setRegtime(new Date());
        Random random=new Random();
        String result1 = "";
        for (int i = 0; i < 5; i++) {
            result1 += random.nextInt(10);
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
        String format = dateFormat.format(new Date());
        format = format + result1;
        customerManage.setMemberCode(format);
        return toAjax(customerManageService.insertCustomerManage(customerManage));
    }

    /**
     * 修改客户管理
     */
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerManage customerManage)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerManage.setStationId(currentUser.getUser().getDeptId());
        customerManage.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(customerManageService.updateCustomerManage(customerManage));
    }

    /**
     * 删除客户管理
     */
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerManageService.deleteCustomerManageByIds(ids));
    }
}
